package com.study.current.step1;

public class ThreadABC {
    //初始化一个执行变量
    private int flag = 0;
    private int i = 10;
    public synchronized void printA() throws InterruptedException{
        while (i > 0){
            if (flag == 0){
                System.out.print("A");
                flag = 1;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized void printB() throws InterruptedException{
        while (i > 0){
            if (flag == 1){
                System.out.print("B");
                flag = 2;
                notifyAll();
            }
            wait();
        }
    }
    public synchronized void printC() throws InterruptedException{
        while (i > 0){
            if (flag == 2){
                System.out.print("C");
                Thread.sleep(1000);
                flag = 0;
                notifyAll();
                i --;
            }
            wait();
        }
    }

    public static void main(String[] args) {
        ThreadABC threadABC = new ThreadABC();
        PrintA a = new PrintA(threadABC);
        PrintB b = new PrintB(threadABC);
        PrintC c = new PrintC(threadABC);
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        Thread t3 = new Thread(c);
        t1.start();
        t2.start();
        t3.start();

    }
}
